// Learn cc.Class.
//  - [Chinese] http.//www.cocos.com/docs/creator/scripting/class.html
//  - [English] http.//www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute.
//  - [Chinese] http.//www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http.//www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks.
//  - [Chinese] http.//www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http.//www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html

var CardClass = cc.Class({
    extends: cc.Component,

    properties: {
        className:"DN_card",
        cardAtlas:{
            default:null,
            type:cc.SpriteAtlas,
        }
        
    },
    statics:{
        getCardValue: function (value){
            if( value == null ){ return null; }
            var i=parseInt(value/4);
            i = i + 1;
            if( i == 14 ){ i = 1; }
            return i;
        },
        getCardColor: function (value){
            if( value == null ){ return null; }
            var t =value%4;
            return t;
        },
    },

    onLoad: function () {
        this.node.thisJS = this;
        this.cardSpr = this.getComponent(cc.Sprite);
    },

    getCardFileName: function (value){
        if( value == null ){ return null; }
        var _ctable = {1:"r",2:"h",3:"m",4:"f"};
        var i=parseInt(value/4),t =value%4;
        i = i + 1;
        if( i == 14 ){ i = 1; }
        var ret = null;
    
        if( i < 10 ){ 
            ret = "poker_"+_ctable[(t+1)]+"0"+i;
        }
        else{ 
            ret= "poker_"+_ctable[(t+1)]+i;
        }
        return ret;
    },
    start: function () {
    },
    // 点数= cards[i]/4+1; 花色= cards[i]%4; 0红 1黑 2梅 3方
    // ctor(paras){
    //     paras = paras || {};
    //     this.value = paras.value;
    //     this.showBack();
    //     this._lightback = cc.Sprite.create();
    //     this.initLightBack();
    //     this._isback = true;

    // },
    getCardValue: function (){
        var i=parseInt(this.value/4),t =this.value%4;
    
        i = i + 1;
        if( i == 14 ){ i = 1; }
    
        this._card_value = i;
        
        return this._card_value;
    },
    
    //显示背面
    showBack: function(){
        this._isback = true
        this.cardSpr.spriteFrame._name = "poker";
    },
    
    //显示正面
    showFront: function (){
        if(!this.value){
            return;
        }
        this._isback = false;
        this.filePath = this.getCardFileName(this.value);
        if( this.filePath ){
            var filePath = this.filePath;
            var sprite = this.cardSpr;

            var frame = this.cardAtlas.getSpriteFrame(filePath);
            sprite.spriteFrame = frame;
        }
    },
    
    
    dark: function () {
        this.node.color = new cc.Color(180, 180, 180);
    },
    
    light: function () {
        this.node.color = new cc.Color(255, 255, 255);
    },
    
    //翻转
    // flip(){
    //     if( this._isback == true ){
    //         var orbitAction = cc.orbitCamera(0.3, 1, 0, 0, -180, 0, 0);
    //         var delayAction = cc.delayTime(0.18);
    //         var callback    = cc.callFunc(handler(this, this.onBackActionComplete_));
    //         var sq          = cc.sequence(delayAction,callback);
    //         var spawn       = cc.spawn(orbitAction,sq);
    //         this.runAction(spawn)
    //         this._isback = false
    //     }
    // },
    flip: function(){
        this.paiNode.rotationX = 0;
        this.paiNode.rotationY = 0;
        var rotationTo = cc.rotateTo(1, 0, 180);
        this.paiNode.runAction(rotationTo);
        this.paiNode.setScaleX(-1);
    },
    update: function (dt) {
        // if(this._isback == true){
        //     if(this.node.rotationY > 90){
        //         this.cardSpr.spriteFrame = this.filePath;
        //     }else{
        //         this.cardSpr.spriteFrame = "poker.png";
        //     }
        // }
    },
    
    //设置value
    setValue: function (value){
        this.value = value;
    },
    
    
    //设置value
    getValue: function (value){
        return this.value;
    },
    
    
    onBackActionComplete_: function (){
        this.setFlipX(true);
        this.showFront();
    },
    
    // refreshConnect (){
    //     Util.delayRun(0.03, function(){
    //         qf.event.dispatchEvent(Niuniu_ET.APPLICATION_ACTIONS_EVENT,{type="show"})
    //     })
    // },
    
    //设置牌高亮背景
    initLightBack: function (){
        this._lightback.setTexture(Niuniu_Games_res.pokerlightback);
        var size = this.node.getContentSize();
        this._lightback.setPosition(size.width/2-4,size.height/2+10);
        this._lightback.setName("LightBack");
        this.addChild(this._lightback);
        this._lightback.setVisible(false);
    },
    
    //显示牌高亮背景
    showLightBack: function (){
        this._lightback.setVisible(true);
    },
    
    //hide牌高亮背景
    hideLightBack: function (){
        this._lightback.setVisible(false);
    },
    
    // 添加监听
    addTouchListener: function( clickedCall, beganCall, movedCall, cancelledCall, tag_ ){
        this.clickedCall = clickedCall ;
        this.beganCall = beganCall ;
        this.movedCall = movedCall ;
        this.cancelledCall = cancelledCall ;
        this.tag_ = tag_;
    
        var listener = cc.EventListenerTouchOneByOne.create();
        listener.setSwallowTouches(true);
        listener.registerScriptHandler(function( touch, event ){
            var beganPos = this.getParent().convertToNodeSpace(touch.getLocation());
            if( cc.rectContainsPoint(this.getBoundingBox(), beganPos) ){
                if( this.beganCall ){
                    return this.beganCall(this, beganPos);
                }
                return true;
            }
            return false;
        }, cc.Handler.EVENT_TOUCH_BEGAN);
        listener.registerScriptHandler(function( touch, event ){
            if( this.movedCall ){
                this.movedCall(this, this.getParent().convertToNodeSpace(touch.getLocation()));
            }
        }, cc.Handler.EVENT_TOUCH_MOVED)
        listener.registerScriptHandler(function( touch, event ){
            if( this.clickedCall ){
                this.clickedCall(this, this.getParent().convertToNodeSpace(touch.getLocation()));
            }
        }, cc.Handler.EVENT_TOUCH_ENDED);
        listener.registerScriptHandler(function( touch, event ){
            if( this.cancelledCall ){
                this.cancelledCall(this);
            }
        }, cc.Handler.EVENT_TOUCH_CANCELLED);
        var eventDispatcher = this.getEventDispatcher();
        eventDispatcher.addEventListenerWithSceneGraphPriority(listener, this);
        this.listener = listener;
    },
    // 设置能否进行触摸监听
    setListenerEnabled: function ( enabled ){
        if( this.listener ){
            this.listener.setEnabled(enabled);
        }
    },
    // 设置是否可以吞噬
    setListenerSwallowEnabled: function( enabled ){
        if( this.listener ){
            this.listener.setSwallowTouches(enabled);
        }
    },
    
});
module.exports = CardClass;